import baseURL from '@/js/common/baseURL'
import axios from 'axios'
export default {
  name: "orderDetail",
  data() {
    return {
      operationType:"view",//页面类型，默认为查看
      id:"",
      orderDetail:{},//订单详情
      countryOptions:[],//国家选项
      foreachIndex : 0,
    }
  },
  methods: {
    getCountryOptions: function() {
      let vm = this;
      axios({
        method: 'POST',
        url: baseURL + '/v1/ebay/meta/country/getList'
      }).then(function (data) {
        if (data.data.success) {
          vm.countryOptions = data.data.data;
          console.log("afterCountry="+vm.orderDetail);
        } else {
          alert(data.data.msg);
        }
      })
    },
    //获取订单详情
    getOrderDetail: function () {
      let vm = this;
      axios({
        method: 'get',
        url: baseURL + '/v1/order/get/'+12
      }).then(function (data) {
        if (data.data.success) {
          vm.orderDetail = data.data.data;
          if (vm.orderDetail) {
            vm.getWarehouseStockBySku(vm.orderDetail.items[vm.foreachIndex].factSku);
          }
          console.log("detail="+vm.orderDetail);
        } else {
          alert(data.data.msg);
        }
      })
    },

    //根据sku获取仓库，库存下拉框
    getWarehouseStockBySku : function (_sku) {
      let vm = this;
      axios({
        method:'post',
        url: baseURL + '/v1/warehouse/getGoodsStockInfoList',
        data: {
          sku:_sku
        }
      }).then(function (data) {
        let wareHouseMap = new Map();
        let wareHouseMapValue = {};
        if (vm.orderDetail.items[vm.foreachIndex]) {
          for (let i=0;i<data.data.data[0].stockInfoVOS.length;i++) {
            //将仓库ID作为key值，存入map中
            if (wareHouseMap.has(data.data.data[0].stockInfoVOS[i].whId)) {
              //先判断有没有此仓库ID，如果有，则做库存累加；
              wareHouseMapValue = wareHouseMap.get(data.data.data[0].stockInfoVOS[i].whId);
              wareHouseMapValue.stockQty = parseInt(wareHouseMapValue.stockQty) + parseInt(data.data.data[0].stockInfoVOS[i].stockQty);
              wareHouseMap.set(data.data.data[0].stockInfoVOS[i].whId, wareHouseMapValue);
            } else {
              let currentWareHouseMapValue = {};
              currentWareHouseMapValue.whName = data.data.data[0].stockInfoVOS[i].whName;
              currentWareHouseMapValue.stockQty = data.data.data[0].stockInfoVOS[i].stockQty;
              wareHouseMap.set(data.data.data[0].stockInfoVOS[i].whId, currentWareHouseMapValue);
            }
            // wareHouseMap.set(data.data.data[0].stockInfoVOS[i].whId, wareHouseMapValue);
          }
          vm.orderDetail.items[vm.foreachIndex].wareHouseOptions = [];
          let currentItem = vm.orderDetail.items[vm.foreachIndex];
          wareHouseMap.forEach((value,key)=>{
            let wareHouseOption = {};
            wareHouseOption.whId = key;
            wareHouseOption.whName = value.whName;
            wareHouseOption.stockQty = value.stockQty;
            currentItem.wareHouseOptions.push(wareHouseOption);
          });
          vm.$set(vm.orderDetail.items, vm.foreachIndex, currentItem);
          vm.foreachIndex++;
          if (vm.orderDetail.items[vm.foreachIndex]) {
            vm.getWarehouseStockBySku(vm.orderDetail.items[vm.foreachIndex].stock.sku);
          } else {
            console.log("afterPush="+vm.orderDetail);
          }
        } else {

        }
      })
      console.log("最终订单详情="+JSON.stringify(vm.orderDetail));
    },
    commit : function () {
      let vm = this;
      let postUrl = "";
      if (vm.operationType=="confirm") {
        //订单确认vm.id
        postUrl = baseURL + '/v1/order/confirm/'+vm.id;
      } else if (vm.operationType=="picking") {
        //配货
        postUrl = baseURL + '/v1/order/prepare/'+vm.id;
      }
      axios({
        method:'post',
        url:postUrl,
        data:vm.orderDetail
      }).then(function (data) {
        if (data.data.success) {
          alert("保存成功");
        } else {
          alert("保存失败");
        }
        console.log(data)
      })
    }
  },

  created() {
    this.operationType = this.$route.query.operationType;
    this.id = this.$route.query.id;
    this.getCountryOptions();
    this.getOrderDetail();
  }
}
